#include<stdio.h>
#include<string.h>
#define BLOCK_SIZE 512

struct file_descriptor
{
char file_name[11];
//char Location_path[30];
char file_type[5];
unsigned long int file_size;
unsigned long int start_block_address;
unsigned int dir_descriptors;
};
void create_file(char *,char *);
void list_files();
FILE *fp;

struct block
{
//bool free;
char data[BLOCK_SIZE-4];
unsigned long int next;
};
void create_blocks();
void list_block();
//void get_addresses();

void main()
{
char i;
fp=fopen("new","wb");
create_blocks();
rewind(fp);
create_file("hello","hi");

//fseek(fp,sizeof(struct file_descriptor),0);
create_file("bye","what");
//rewind(fp);

fclose(fp);	
fp=fopen("new","rb+");
list_block();
rewind(fp);

list_files();
//fseek(fp,sizeof(struct file_descriptor),0);
list_files();
fclose(fp);	


fp=fopen("new","rb+");
fseek(fp,2*sizeof(struct file_descriptor),0);
create_file("file3","what u doin");
fclose(fp);
fp=fopen("new","rb+");
list_block();
//fclose(fp);
rewind(fp);
//fp=fopen("new","rb");
list_files();
//fclose(fp);
//fp=fopen("new","rb");
//fseek(fp,sizeof(struct file_descriptor),0);
list_files();
list_files();
//create_file("file3","what u doin");
//fseek(fp,-sizeof(struct file_descriptor),0);
//list_files();

fclose(fp);	

}

void create_blocks()
{

//unsigned long int i,temp;
//while(ftell(fp)<m.vfs_size)
//{
	struct block b;
	b.next=BLOCK_SIZE;
	fwrite(&b,sizeof(struct block),1,fp);
//}

}

void list_block()
{
	struct block b;	
	fread(&b,sizeof(struct block),1,fp);
		printf("\ndata:%s",b.data);		
		printf("\naddress:%ld",b.next);


}
void create_file(char *name,char *content)
{
	//FILE *fp;	
	
	struct file_descriptor f;
	//struct block b;
	
	//unsigned long int temp;
	//temp=m.free_address;	
	//fseek(fp,m.free_address,0);
	//fread(&b,sizeof(struct block),1,fp);
	//m.free_address=b.next;
	
	//strcpy(b.data,content);
	//b.next=0xFFFF;	
	
	strcpy(f.file_name,name);
	//printf("\nname:%s\n",f.file_name);	
	strcpy(f.file_type,"txt");
	f.file_size=strlen(content);
	f.start_block_address=0;
	f.dir_descriptors=0;
	
	//fseek(fp,sizeof(struct main_header)+(m.root_descriptors)*sizeof(struct file_descriptor),0);
	fwrite(&f,sizeof(struct file_descriptor),1,fp);
	//fseek(fp,temp,0);
	//fwrite(&b,sizeof(struct block),1,fp);
	//m.root_descriptors++;
	//m.fd_used++;
	
	//update_main_header();
}
void list_files()
{	//FILE *fp;
	
	int i;
	struct file_descriptor f;
	//struct block b;	
	//fseek(fp,sizeof(struct main_header),0);
	
	//for(i=0;i<m.root_descriptors;i++)
	//{
		fread(&f,sizeof(struct file_descriptor),1,fp);
		printf("\nfile %d:",i);		
		printf("\naddress:%ld",f.start_block_address);
		printf("\nname:%s",f.file_name);
		printf("\nsize:%ld",f.file_size);
		printf("\ntype:%s",f.file_type);
		//printf("\nvfs_label:%s",m.vfs_label);
		
	//}
	//fclose(fp);
}
